using System;
using System.Reflection;
using UnityEngine;

public class Stretcher : MonoBehaviour
{
	private void Start()
	{
		if (base.GetComponent<Renderer>() == null)
		{
			return;
		}
		object dimensionObject = this.GetDimensionObject();
		if (dimensionObject == null)
		{
			this.StretchScale();
		}
		else
		{
			this.StretchDimensions(dimensionObject);
		}
	}

	private void StretchDimensions(object obj)
	{
		Vector2 propValue = this.GetPropValue<Vector2>(obj, "dimensions");
		Vector2 val = propValue;
		Vector2 vector = (!(this.cam.InheritConfig == null)) ? this.cam.InheritConfig.NativeResolution : this.cam.NativeResolution;
		tk2dCameraResolutionOverride.AutoScaleMode autoScaleMode = this.cam.CurrentResolutionOverride.autoScaleMode;
		if (autoScaleMode != tk2dCameraResolutionOverride.AutoScaleMode.FitWidth)
		{
			if (autoScaleMode == tk2dCameraResolutionOverride.AutoScaleMode.FitHeight)
			{
				float num = vector.y / this.cam.TargetResolution.y;
				STRETCH_SIDE stretch_SIDE = this.side;
				if (stretch_SIDE != STRETCH_SIDE.VIRTICAL)
				{
					if (stretch_SIDE != STRETCH_SIDE.HORIZONTAL)
					{
						if (stretch_SIDE == STRETCH_SIDE.BOTH)
						{
							val.x = this.cam.TargetResolution.x * num;
							val.y = vector.y;
						}
					}
					else
					{
						val.x = this.cam.TargetResolution.x * num;
						if (this.rateMaintain)
						{
							val.y = propValue.y * (val.x / propValue.x);
						}
					}
				}
				else
				{
					val.y = vector.y;
				}
			}
		}
		else
		{
			float num2 = vector.x / this.cam.TargetResolution.x;
			STRETCH_SIDE stretch_SIDE2 = this.side;
			if (stretch_SIDE2 != STRETCH_SIDE.VIRTICAL)
			{
				if (stretch_SIDE2 != STRETCH_SIDE.HORIZONTAL)
				{
					if (stretch_SIDE2 == STRETCH_SIDE.BOTH)
					{
						val.x = vector.x;
						val.y = this.cam.TargetResolution.y * num2;
					}
				}
				else
				{
					val.x = vector.x;
					if (this.rateMaintain)
					{
						val.y = propValue.y * (vector.x / propValue.x);
					}
				}
			}
			else if (this.rateMaintain)
			{
				val.y = this.cam.TargetResolution.y * num2;
			}
		}
		this.SetPropValue<Vector2>(obj, "dimensions", val);
	}

	private void StretchScale()
	{
		if (base.GetComponent<Renderer>() == null)
		{
			return;
		}
		base.transform.localScale = Vector3.one;
		tk2dBaseSprite component = base.GetComponent<tk2dBaseSprite>();
		if (component != null)
		{
			component.scale = Vector3.one;
		}
		Vector3 size = base.GetComponent<Renderer>().bounds.size;
		float y = this.cam.ScreenExtents.height / size.y;
		float x = this.cam.ScreenExtents.width / size.x;
		if (component != null)
		{
			component.scale = new Vector3(x, y, 1f);
		}
		else
		{
			base.transform.localScale = new Vector3(x, y, 1f);
		}
	}

	private object GetDimensionObject()
	{
		if (base.GetComponent<tk2dBaseSprite>() == null)
		{
			return null;
		}
		tk2dSlicedSprite component = base.GetComponent<tk2dSlicedSprite>();
		if (component != null)
		{
			return component;
		}
		tk2dTiledSprite component2 = base.GetComponent<tk2dTiledSprite>();
		if (component2 != null)
		{
			return component2;
		}
		return null;
	}

	private T GetPropValue<T>(object target, string name)
	{
		FieldInfo field = target.GetType().GetField(name);
		if (field != null)
		{
			return (T)((object)field.GetValue(target));
		}
		PropertyInfo property = target.GetType().GetProperty(name);
		if (property != null)
		{
			return (T)((object)property.GetValue(target, null));
		}
		return default(T);
	}

	private void SetPropValue<T>(object target, string name, T val)
	{
		FieldInfo field = target.GetType().GetField(name);
		if (field != null)
		{
			field.SetValue(target, val);
		}
		PropertyInfo property = target.GetType().GetProperty(name);
		if (property != null)
		{
			property.SetValue(target, val, null);
		}
	}

	public tk2dCamera cam;

	public STRETCH_SIDE side = STRETCH_SIDE.BOTH;

	public bool rateMaintain = true;
}
